//
//  DDRegAndLogViewModel.m
//  DingDongXiChe
//
//  Created by wtjr on 2018/1/2.
//  Copyright © 2018年 xieyingze. All rights reserved.
//

#import "DDRegAndLogViewModel.h"

@implementation DDRegAndLogViewModel

- (void)initialize {
    
}

#pragma mark - Method

/**
 注册按钮是否可以点击信号
 */
- (RACSubject *)registerButtonAvailableSubject {
    if (!_registerButtonAvailableSubject) {
        _registerButtonAvailableSubject = [RACSubject subject];
    }
    return _registerButtonAvailableSubject;
}

/**
 登录按钮是否可以点击信号
 */
- (RACSubject *)loginButtonAvailableSubject {
    if (!_loginButtonAvailableSubject) {
        _loginButtonAvailableSubject = [RACSubject subject];
    }
    return _loginButtonAvailableSubject;
}

/**
 点击注册按钮信号
 */
- (RACSubject *)gotoRegisterSubject {
    if (!_gotoRegisterSubject) {
        _gotoRegisterSubject = [RACSubject subject];
    }
    return _gotoRegisterSubject;
}

/**
 点击登录按钮信号
 */
- (RACSubject *)gotoLoginSubject {
    if (!_gotoLoginSubject) {
        _gotoLoginSubject = [RACSubject subject];
    }
    return _gotoLoginSubject;
}

#pragma mark - Getter
/**
 注册请求信号
 */
- (RACCommand *)gotoRegisterCommand {
    if (!_gotoRegisterCommand) {
        _gotoRegisterCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                NetworkParam *param = [NetworkParam instance];
                param.isPOST = YES;
                param.methodName = kURL_register;
                param.isSHowLoading = YES;
                param.paramArr = [NSMutableArray arrayWithArray:@[
                                                                  kMethod_param(@"user_phone", self.registerPhone),
                                                                  kMethod_param(@"user_password", self.registerPSW)
                                                                  ]];
                [self.request networkRequestWithNetworkParam:param completed:^(BOOL isSucess, NSString *resultString) {
                    if (isSucess) {
                        [subscriber sendNext:resultString];
                        [subscriber sendCompleted];
                    } else {
                        [subscriber sendError:[NSError errorWithDomain:resultString code:0 userInfo:nil]];
                        [self showAlertViewWithMessage:resultString];
                        [subscriber sendCompleted];
                    }
                }];
                return nil;
            }];
        }];
    }
    return _gotoRegisterCommand;
}

/**
 登录请求信号
 */
- (RACCommand *)gotoLoginCommand {
    if (!_gotoLoginCommand) {
        _gotoLoginCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
            return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
                
                NetworkParam *param = [NetworkParam instance];
                param.methodName = kURL_login;
                param.isSHowLoading = NO;
                param.paramArr = [NSMutableArray arrayWithArray:@[
                                                                  kMethod_param(@"user_phone", self.loginPhone),
                                                                  kMethod_param(@"user_password", self.loginPSW)
                                                                  ]];
                [self.request networkRequestWithNetworkParam:param completed:^(BOOL isSucess, NSString *resultString) {
                    if (isSucess) {
                        [subscriber sendNext:resultString];
                        [subscriber sendCompleted];
                    } else {
                        [subscriber sendError:[NSError errorWithDomain:resultString code:0 userInfo:nil]];
                        [self showAlertViewWithMessage:resultString];
                        [subscriber sendCompleted];
                    }
                }];
                return nil;
            }];
        }];
    }
    return _gotoLoginCommand;
}


@end
